<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Ajax_controller extends CI_Controller {
	/**
		*	check_db: checks connection to database
		*
		*	@author:	Philipp Panka	<ppanka@gedak.de>
		*/
	public function check_db(){
		if($this->input->post('server') AND $this->input->post('username') AND $this->input->post('password') AND $this->input->post('database')){
			$this->load->model('install_model');
			if($this->install_model->check_db($this->input->post('server'), $this->input->post('username'), $this->input->post('password'), $this->input->post('database'))){
				echo 'setMessage(\'success\', \'Verbindung zur Datenbank hergestellt\');';
			}else{
				echo 'setMessage(\'error\', \'Verbindung zur Datenbank konnte nicht hergestellt werden\');';
			}
		}else{
			echo 'setMessage(\'error\', \'Bitte alle Felder ausfüllen!\');';
		}
	}
	
	/**
		*	install_database: install database
		*
		*	@author:	Philipp Panka	<ppanka@gedak.de>
		*/
	public function install_database(){
		//get data from session
		$db_data = $this->session->userdata('database');
		//load sql file
		$sql_no_data = file_get_contents('../db/installation/basic_data.sql.txt');
		//replace database placeholder with database name
		$sql_no_data = str_replace('{{DATABASE}}', $db_data['database'], $sql_no_data);
		$sql_no_data = str_replace("\n", "", $sql_no_data);
		$connection = mysqli_connect($db_data['server'], $db_data['username'], $db_data['password']);
			
		if(!mysqli_query($connection, $sql_no_data)){
			var_dump( mysqli_error($connection));
		}
		
		//load kbo db_conf file and replace placeholder with db connecting data
		$db_conf = file_get_contents('../includes/inc.db.php');
		$db_conf = str_replace('{{SERVER}}', $db_data['server'], $db_conf);
		$db_conf = str_replace('{{USER}}', $db_data['username'], $db_conf);
		$db_conf = str_replace('{{PASSWORD}}', $db_data['password'], $db_conf);
		$db_conf = str_replace('{{DATABASE}}', $db_data['database'], $db_conf);
		file_put_contents('../includes/inc.db.php', $db_conf);
	}
	
	/**
		*	install_basic_data: install basic data
		*
		*	@author:	Philipp Panka	<ppanka@gedak.de>
		*/
	public function install_basic_data(){
		//get data from session
		$db_data = $this->session->userdata('database');
		$admin_data = $this->session->userdata('administrator');
		$school_data = $this->session->userdata('school');
		//load sql file
		$sql_basic_data = file_get_contents('../db/installation/basic_data.sql.txt');
		//replace database placeholder with database name
		$sql_basic_data = str_replace('{{DATABASE}}', $db_data['database'], $sql_basic_data);
		$sql_basic_data = str_replace('{{SCHOOLNAME}}', $school_data['schoolname'], $sql_basic_data);
		$sql_basic_data = str_replace('{{SCHOOLMAIL}}', $school_data['schoolmail'], $sql_basic_data);
		$sql_basic_data = str_replace('{{KBOMAIL}}', $school_data['kbomail'], $sql_basic_data);
		$sql_basic_data = str_replace('{{SENDER}}', $school_data['sender'], $sql_basic_data);
		$sql_basic_data = str_replace('{{ADMINMAIL}}', $admin_data['email'], $sql_basic_data);
		$sql_basic_data = str_replace('{{PASSWORD}}', md5($admin_data['admin_password']), $sql_basic_data);
		$sql_basic_data = str_replace("\n", "", $sql_basic_data);
		$connection = mysqli_connect($db_data['server'], $db_data['username'], $db_data['password']);
		if(!mysqli_query($connection, $sql_basic_data));{
			echo mysqli_error($connection);
		}
		//clear session
		$this->session->sess_destroy();
	}
	
}